<?php
/**
 * @author gabriel
 */
class Gallery_Model_Gallery extends Promotor_Model_Scaffold {
	/**
	 * @var string
	 */
	protected $_dbTableClass = 'Gallery_Model_DbTable_Gallery';
	
	protected $_cachedMethods = array(
		'fetchAllImagesById',
		'fetchAllImagesGroupAlbum',
		'fetchAllImagesGroupAlbumById',
		'fetchAllInline'
	);

	/**
	 * @param Zend_Db_Select $select
	 * @return Zend_Db_Select
	 */
	protected function _selectImages(Zend_Db_Select $select = null) {
		if (null === $select) {
			$table = $this->getDbTable();
			$db = $table->getAdapter();
			$select = new Zend_Db_Select($db);
		}

		$select->from(array('gi' => 'gallery_image'))
			   ->joinInner(array('ga' => 'gallery_album'),'ga.publicated = 1 AND ga.id = gi.album_id', array('album_name' => 'ga.name','album_description' => 'ga.description'))
			   // nie wyswietla pustych galerii - aby były puste "RIGHT"
			   ->joinInner(array('g' => 'gallery'),'g.publicated = 1 AND g.id = ga.gallery_id', array('gallery_name' => 'g.name','gallery_description' => 'g.description'))
			   ->where('gi.publicated = 1')
			   ->order('g.idx ASC')
			   ->order('gi.idx ASC');

		return $select;
	}
	
	/**
	 * @param Zend_Db_Select $select
	 * @return Zend_Db_Select
	 */
	protected function _selectMedia(Zend_Db_Select $select = null) {
		if (null === $select) {
			$table = $this->getDbTable();
			$db = $table->getAdapter();
			$select = new Zend_Db_Select($db);
		}

		$select->from(array('gm' => 'gallery_media'))
			   ->joinInner(array('ga' => 'gallery_album'),'ga.publicated = 1 AND ga.id = gm.album_id', array('album_name' => 'ga.name','album_description' => 'ga.description'))
			   // nie wyswietla pustych galerii - aby były puste "RIGHT"
			   ->joinInner(array('g' => 'gallery'),'g.publicated = 1 AND g.id = ga.gallery_id', array('gallery_name' => 'g.name','gallery_description' => 'g.description'))
			   ->where('gm.publicated = 1')
			   ->order('g.idx ASC')
			   ->order('gm.idx ASC');

		return $select;
	}

	/**
	 * @param integer $id
	 * @param Zend_Db_Select $select
	 * @return Zend_Db_Select
	 */
	protected function _selectInline($id, Zend_Db_Select $select = null) {
		$table = $this->getDbTable();
		$adapter = $table->getAdapter();

		$select1 = new Zend_Db_Select($adapter);
		$select1->from(
					array('gm' => 'gallery_media'),
					array(
						'id',
						'name',
						'description',
						'image',
						'media',
						'type' => new Zend_Db_Expr("'media'"),
						'idx'))

				->joinInner(
					array('ga' => 'gallery_album'),
					'ga.publicated = 1 AND ga.id = gm.album_id',
	 				array(
	 					'album_id' => 'ga.id',
	 					'album_name' => 'ga.name',
	 					'album_description' => 'ga.description'))
	 				
	 			->joinInner(
	 				array('g' => 'gallery'),
	 				'g.publicated = 1 AND g.id = ga.gallery_id',
	 				array(
	 					'gallery_id' => 'g.id',
	 					'gallery_idx' => 'g.idx',
	 					'gallery_name' => 'g.name',
	 					'gallery_description' => 'g.description'))

				->where('gm.publicated = ?', 1);;

		$select2 = new Zend_Db_Select($adapter);
		$select2->from(
					array('gi' => 'gallery_image'),
					array(
						'id',
						'name',
						'description',
						'image',
						'media' => new Zend_Db_Expr('NULL'),
						'type' => new Zend_Db_Expr("'image'"),
						'idx'))
				->joinInner(
					array('ga' => 'gallery_album'),
					'ga.publicated = 1 AND ga.id = gi.album_id',
	 				array(
	 					'album_id' => 'ga.id',
	 					'album_name' => 'ga.name',
	 					'album_description' => 'ga.description'))

	 			->joinInner(
	 				array('g' => 'gallery'),
	 				'g.publicated = 1 AND g.id = ga.gallery_id',
	 				array(
	 					'gallery_id' => 'g.id',
	 					'gallery_idx' => 'g.idx',
	 					'gallery_name' => 'g.name',
	 					'gallery_description' => 'g.description'))

			 	->where('gi.publicated = ?', 1);
			 	
		if (is_numeric($id)) {
			$select1->where('g.id = ?', $id);
			$select2->where('g.id = ?', $id);
		}
		
		$union = array($select1, $select2);

		$select = new Zend_Db_Select($adapter);
		$select->union($union)
			   ->order('gallery_idx ASC')
			   ->order('idx ASC');
		
		return $select;
	}
	
	/**
	 * @param integer $id
	 * @param Zend_Db_Select $select
	 * @return array
	 */
	public function fetchAllImagesById($id, Zend_Db_Select $select = null) {
		$select = $this->_selectImages($select);
		$select->where('g.id = ?', $id, Zend_Db::INT_TYPE);

		try {
			$stmt = $select->query();
			$result = $stmt->fetchAll();
			$this->_setStatus(self::SUCCESS);
			return $result;
		} catch (Zend_Db_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addMessage($e);
		}
	}
	
	/**
	 * @param integer $id
	 * @param Zend_Db_Select $select
	 * @return array
	 */
	public function fetchAllImagesGroupAlbum(Zend_Db_Select $select = null) {
		$select = $this->_selectImages($select);
		$select->group('ga.id')
			   ->having('gi.publicated = 1')
			   ->columns(array('albums' => new Zend_Db_Expr('COUNT(ga.id)')));

		try {
			$stmt = $select->query();
			$result = $stmt->fetchAll();
			$this->_setStatus(self::SUCCESS);
			return $result;
		} catch (Zend_Db_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addMessage($e);
		}
	}
	
	/**
	 * @param integer $id
	 * @param Zend_Db_Select $select
	 * @return array
	 */
	public function fetchAllImagesGroupAlbumById($id, Zend_Db_Select $select = null) {
		$select = $this->_selectImages($select);
		$select->where('g.id = ?', $id, Zend_Db::INT_TYPE)
			   ->group('ga.id')
			   ->columns(array('g.albums' => new Zend_Db_Expr('COUNT(ga.id)')));

		try {
			$stmt = $select->query();
			$result = $stmt->fetchAll();
			$this->_setStatus(self::SUCCESS);
			return $result;
		} catch (Zend_Db_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addMessage($e);
		}
	}

	/**
	 * @param integer $id
	 * @param Zend_Db_Select $select
	 * @return array
	 */
	public function fetchAllInline($id = null, Zend_Db_Select $select = null) {
		$rowset = array();

		$select = $this->_selectInline($id, $select);
		
		try {
			$stmt = $select->query();
			
			while ($row = $stmt->fetch()) {
				$galleryKey = $row['gallery_id'];

				// kolekcjonowanie galerii
				if (!isset($rowset[$galleryKey])) {
					$rowset[$galleryKey] = array(
						'id'			=> $row['gallery_id'],
						'name' 			=> $row['gallery_name'],
						'description'	=> $row['gallery_description'],
						'albums' 		=> array()
					);
				}

				$albumKey = $row['album_id'];

				// kolekcjonowanie albumów w galerii
				if (!isset($rowset[$galleryKey]['albums'][$albumKey])) {
					$rowset[$galleryKey]['albums'][$albumKey] = array(
						'id' 			=> $row['album_id'],
						'name'			=> $row['album_name'],
						'description' 	=> $row['album_description'],
						'image' 		=> $row['image'],
					
						// określenie typu, pierwszego elementu w albumie..
						'type' => strlen($row['media']) ? 'media' : 'image'
					);
				}
			}

			$this->_setStatus(self::SUCCESS);
		} catch (Zend_Db_Exception $e) {
			$this->_addMessage($e);
			$this->_setStatus(self::FAILURE);
		}

		/**
		 * Gdy poda parametr $id i galeria została odnaleziona
		 * zwróć galerię jako rekord
		 */ 
		if (is_numeric($id)) {
			return current($rowset);
		}
		
		return $rowset;
	}
	
	/**
	 * @return array
	 */
	public function fetchAll() {
		$table = $this->getDbTable();
		try {
			$rowset = $table->fetchAll();
			$this->_setStatus(self::SUCCESS);
			return $rowset->toArray();
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addMessage($e);
		}
	}
	
	/**
	 * @param integer $id
	 * @return array
	 */
	public function findById($id) {
		$table = $this->getDbTable();
		try {
			$rowset = $table->find((int) $id);
			if(!count($rowset)) {
				$this->_setStatus(self::FAILURE);
				return;
			}
			$this->_setStatus(self::SUCCESS);
			return $rowset->current()->toArray();
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addMessage($e);
		}
	}
}